<?php
namespace app\controller\back;

use app\common\controller\_AdminController;
use app\model\Permission;
use think\Validate;

class PermissionController extends _AdminController
{
	protected $rule = [
		'id|编号'			=> 'integer',
		'pid|上级编号'		=> 'integer',
		'name|名称'			=> 'chsDash|length:2,50',
		'path|路径'			=> 'regex:^\/(\w+\/?)+$|length:3,100',
		'desc|描述'			=> 'chsDash|max:200',
		'sort|排序'			=> 'integer',
		'status|状态'		=> 'in:0,1',
	];

	public $scene = [
		'create'			=> ['pid','name','path','desc','status','sort'],
		'update'			=> ['id','pid','name','path','desc','status','sort'],
		'delete'			=> ['id'],
		'list'				=> [],
	];

	protected function initialize()
	{	
		parent::initialize();
		switch ($this->action) {
			case 'create':
				$this->appendRule('pid','checkPermissionByPid');
				$this->appendRule('name','require|unique:permission');
				$this->appendRule('path','requireWith:pid|unique:permission');
				break;
			case 'update':
				$this->appendRule('id','checkPermissionById');
				$this->appendRule('pid','checkPermissionByPid');
				$this->appendRule('name','unique:permission');
				$this->appendRule('path','requireWith:pid|unique:permission');
				break;
            case 'delete':
                $this->appendRule('id','require|checkPermissionDel');
                break;
			default:
				# code...
				break;
		}
		
		$this->check();
	}

	/**
	 * @api {POST} /back/permission/create 添加路由
	 * @apiVersion 0.0.1
	 * @apiGroup Back-permission
	 *
	 * @apiParam {Number} pid 组编号
	 * @apiParam {String} name 名称
	 * @apiParam {String} path 路径
	 * @apiParam {String} [desc] 描述
	 * @apiParam {Number=0禁用,1启用} [status=1] 状态
	 *
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "pid": 1,
	 *     "name": "管理员管理",
	 *     "pid": "1",
	 *     "path": "/back/permission/create",
	 *     "desc": "描述",
	 *     "status": 1
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function createAction()
	{
		$this->data['path'] = !empty($this->data['pid'])?$this->data['path']:'';
		Permission::doSave($this->data);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/permission/update 修改路由
	 * @apiVersion 0.0.1
	 * @apiGroup Back-permission
	 *
	 * @apiParam {Number} id 编号
	 * @apiParam {Number} [pid] 组编号
	 * @apiParam {String} [name] 名称
	 * @apiParam {String} [path] 路径
	 * @apiParam {String} [desc] 描述
	 * @apiParam {Number=0禁用,1启用} [status] 状态
	 *
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "id": 1,	
	 *     "pid": 1,
	 *     "name": "管理员管理",
	 *     "path": "/back/permission/create",
	 *     "desc": "描述",
	 *     "status": 1
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function updateAction()
	{   
		Permission::doSave($this->data,$this->id);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/permission/delete 删除路由
	 * @apiVersion 0.0.1
	 * @apiGroup Back-permission
	 *
	 * @apiParam {Number} id 编号
	 *
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "id": 1,	
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function deleteAction()
	{   
		Permission::doDelete($this->id);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/permission/list 路由列表
	 * @apiVersion 0.0.1
	 * @apiGroup Back-permission
	 * 
	 * @apiParam {Number} [page=1] 页码
	 * @apiParam {Number} [limit=10] 每页条数
	 *
	 * @apiSuccess {Number} id 路由组编号
	 * @apiSuccess {String} name 路由组名称
	 * @apiSuccess {Number} sort 路由组排序
	 * @apiSuccess {Number} status 路由组状态码
	 * @apiSuccess {Number} status 路由组状态值
	 * @apiSuccess {Object[]} sub 路由组下的路由
	 * @apiSuccess {Number} sub.id 路由编号
	 * @apiSuccess {String} sub.name 路由名称
	 * @apiSuccess {String} sub.path 路由路径
	 * @apiSuccess {String} sub.desc 路由描述
	 * @apiSuccess {Number} sub.status 路由状态码
	 * @apiSuccess {Number} sub.status_text 路由状态值
	 * @apiSuccess {String} sub.create_time 路由创建时间
	 * @apiSuccess {String} sub.update_time 路由更新时间
	 * @apiSuccess {Number} sub.pid 路由组编号
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": {
	 *         "list": [
	 *             {
	 *                 "id": 2,
	 *                 "name": "角色管理",
	 *                 "sort": 0,
	 *                 "status": 1,
	 *                 "status_text": "正常"
	 *                 "sub": [
	 *                     {
	 *                         "id": 6,
	 *                         "name": "添加路由",
	 *                         "path": "/admin/permission/add",
	 *                         "desc": "",
	 *                         "status": 1,
	 *                         "create_time": "2019-03-11 16:56:57",
	 *                         "update_time": "2019-03-11 16:56:57",
	 *                         "pid": 2,
	 *                         "status_text": "正常"
	 *                     },
	 *                 ]
	 *             }
	 *         ]
	 *     }
	 *}
	 */
	public function listAction()
	{   
		$list = Permission::doList();
		return parent::resSuccess($list);
	}

	// 自定义规则
	protected function checkPermissionByPid()
	{
		Validate::extend('checkPermissionByPid', function ($value) {
			if($value){
				$permission = Permission::where(['pid'=>0,'id'=>$value])->find();
				return $permission?true:'上级不存在';
			}
			return true;
		});
	}

	// 自定义规则
	protected function checkPermissionById()
	{
		Validate::extend('checkPermissionById', function ($value) {
			$permission = Permission::get($value);
			return $permission?true:'记录不存在';
		});
	}

	// 自定义规则
	protected function checkPermissionDel()
	{
		Validate::extend('checkPermissionDel', function ($value) {
			$permission = Permission::get($value);
			if(!$permission){
				return '记录不存在';
			}
			if($permission->status == Permission::STATUS_ENABLE){
				return '删除前请先禁用';
			}
			if($permission->pid == 0){
				$sub = Permission::where(['pid'=>$value])->find();
				if($sub){
					return '有下级数据,无法删除';
				}
			}
			return true;
		});
	}
	
}
